/*
 * Project: push-center-service-impl
 * 
 * File Created at 2014-10-10
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.service.core.tmpl;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.greenline.push.core.pushcenter.AppTemplateManager;
import com.greenline.push.core.pushcenter.TemplateManager;
import com.greenline.push.dal.pushcenter.dataobject.AppTemplateDO;
import com.greenline.push.dal.pushcenter.dataobject.TemplateDO;
import com.greenline.push.service.core.utils.Constants;

/**
 * 模板信息的本地缓存
 * 
 * @author weirui.shenwr
 * @date 2014-10-10
 * @version V1.0
 */
public class TemplateCache {
    @Autowired
    private TemplateManager templateManager;
    @Autowired
    private AppTemplateManager appTemplateManager;
    private static final String SPLITS = "%";
    private static final String CODE = "CODES";
    // render : template:能找到 ,则使用. templateId%app_tempalteId 能找到 则优先使用
    // getbycode : code%code 比如:weiyi-patient%code 是根据code寻找tempalte .也做入缓存
    private Map<String, TemplateInfo> data = new ConcurrentHashMap<String, TemplateInfo>();
    private Logger log = Constants.LOG_CACHE;

    @SuppressWarnings("unused")
    private void initial() {
        log.info("TemplateCache initial start=====");
        List<TemplateDO> templateList = templateManager.listAll();
        if (null != templateList) {
            for (TemplateDO tmp : templateList) {
                TemplateInfo info = new TemplateInfo();
                info.setId(tmp.getId());
                info.setPriority(tmp.getPriority());
                info.setText(tmp.getText());
                info.setTitle(tmp.getTitle());
                info.setParams(tmp.getParams());
                data.put(tmp.getId().toString(), info);
                data.put(tmp.getCode() + SPLITS + CODE, info);
                // the following lines is used by if apptemplate has its self
                // template.[text]
                AppTemplateDO query = new AppTemplateDO();
                query.setTemplateId(tmp.getId());
                List<AppTemplateDO> tmpappTemplate = appTemplateManager.listByExample(query);
                for (AppTemplateDO appTp : tmpappTemplate) {
                    if (StringUtils.isNotBlank(appTp.getText())) {
                        TemplateInfo tappTemplate = new TemplateInfo();
                        tappTemplate.setText(appTp.getText());
                        data.put(tmp.getId() + SPLITS + appTp.getId(), tappTemplate);
                    }
                }
            }
        }
        log.info("TemplateCache initial end=====");
    }

    /**
     * 重新加载模板配置
     */
    public void reload() {
        List<TemplateDO> templateList = templateManager.listAll();
        // 正常调用不应为null
        if (null != templateList) {
            // set modified
            Entry<String, TemplateInfo> firstElement = data.entrySet().iterator().next();
            long currTime = 0;
            if (firstElement != null) {
                TemplateInfo tinfo = firstElement.getValue();
                currTime = tinfo.getLastModified() + 1;
            }
            // clear
            data.clear();
            // put
            for (TemplateDO tmp : templateList) {
                TemplateInfo info = new TemplateInfo();
                info.setId(tmp.getId());
                info.setPriority(tmp.getPriority());
                info.setText(tmp.getText());
                info.setTitle(tmp.getTitle());
                info.setParams(tmp.getParams());
                info.setLastModified(currTime);// add date
                                               // stamp
                data.put(tmp.getId().toString(), info);
                data.put(tmp.getCode() + SPLITS + CODE, info);
                // the following lines is used by if apptemplate has its self
                // template.[text]
                AppTemplateDO query = new AppTemplateDO();
                query.setTemplateId(tmp.getId());
                List<AppTemplateDO> tmpappTemplate = appTemplateManager.listByExample(query);
                for (AppTemplateDO appTp : tmpappTemplate) {
                    if (StringUtils.isNotBlank(appTp.getText())) {
                        TemplateInfo tappTemplate = new TemplateInfo();
                        tappTemplate.setText(appTp.getText());
                        tappTemplate.setLastModified(currTime);
                        data.put(tmp.getId() + SPLITS + appTp.getId(), tappTemplate);
                    }
                }
            }
        }
    }

    public TemplateInfo get(String templateId) {
        return data.get(templateId);
    }

    public TemplateInfo getByCode(String code) {
        return data.get(code + SPLITS + CODE);
    }

}
